<template>
  <div class="service-commission">
    <tags :labels="labels" :current-label="currentLabel" @clickLabel="clickLabel" class="tag">
      <ul class="list">
        <template v-for="(item, index) in list">
          <li
            :key="index"
            class="list-item border-bottom-1px"
            v-if="+item.balance > 0"
          >
            <div class="award">
              {{ item.award_name }}
            </div>
            <div class="desc">
              <p class="time">返佣时间: {{ item.create_time }}</p>
              <p class="time">订单ID: {{ item.order_id }} </p>
              <p class="time">佣金ID: {{item.id}} </p>
              <p class="amount">佣金: <span>￥{{ +item.balance }}</span></p>
              <p class="time" v-if="false">积分: {{ +item.score }}</p>
              <p class="time" v-if="false">蜜易券: {{ +item.platform_ticket }}</p>
              <p class="time" v-if="false">购物券: {{ +item.ticket }}</p>
              <p class="time">状态: {{ item.status | statusText }}</p>
            </div>
          </li>
        </template>
      </ul>
      <pull-up
        :loading="loading"
        :busy="queryList.busy"
        @loadMore="loadMore" />
      <loading :show="loading" />
    </tags>
  </div>
</template>

<script>
import Tags from '../components/Tags'
import Loading from '@/components/Loading'
import api from '@/api'
import base64 from '@/util/base64'
import PullUp from '@/components/PullUp'
export default {
  name: 'ServiceCommission',
  components: {
    Tags,
    Loading,
    PullUp
  },
  computed: {
    currentLabel() {
      return this.labels[this.current]
    }
  },
  filters: {
    statusText(v) {
      return +v === 1 ? '已返' : '待返'
    }
  },
  data() {
    return {
      labels: [],
      current: 0,
      list: [],
      loading: false,
      queryList: {}
    }
  },
  methods: {
    loadMore() {
      setTimeout(() => {
        this.getList(true)
      }, 200)
    },
    getList(append = false) {
      if (this.queryList.busy === false) {
        this.queryList.page++
        this.loading = true
        this.queryList.busy = true
        api.My.commissionList({
          ...this.queryList,
          type: this.current
        }).then(response => {
          let { data } = response
          data = base64.parse(data)
          if (data.list.length === 0) {
            this.queryList.busy = true
          } else {
            this.queryList.busy = false
            if (append) {
              this.list = this.list.concat(data.list)
            } else {
              this.list = data.list
            }
          }
        }).finally(() => {
          this.loading = false
        })
      }
    },
    createTags () {
      const tags = []
      tags[0] = '全部'
      tags[1] = '已返'
      tags[2] = '待返'
      this.labels = tags
    },
    clickLabel(index) {
      this.current = index
      this.init()
      this.list = []
      this.getList()
    },
    init() {
      this.queryList = {
        page: 0,
        pageSize: 10,
        busy: false
      }
    }
  },
  created () {
    this.current = Number.parseInt(this.$route.params.type || 0)
    this.createTags()
    this.init()
    this.getList()
  },
  deactivated () {
    this.queryList.busy = true
  }
}
</script>

<style scoped lang="stylus">
.service-commission
  background white
  padding-bottom 30px
  border-radius 10px
  .list
    margin-bottom 15px
    .list-item
      display flex
      padding 10px
      margin-bottom 10px
      .award
        width 100px
        line-height 100px
      .desc
        flex 1
        text-align left
        p
          //height 25px
          line-height 25px
      .amount
        span
          color red
          font-weight 700
          font-size 16px
</style>
